package v1

import (
	"gin-demo/models"
	customValidator "gin-demo/pkg/customValidator"
	"gin-demo/pkg/e"
	"gin-demo/pkg/setting"
	"gin-demo/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

// 获取标签列表
func GetTags(c *gin.Context) {
	name := c.Query("name")

	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if name != "" {
		maps["name"] = name
	}

	var state = -1
	if arg := c.Query("state"); arg != "" {
		state = com.StrTo(arg).MustInt()
		maps["state"] = state
	}

	// code := e.SUCCESS

	data["lists"] = models.GetTags(util.GetPage(c), setting.PageSize, maps)
	data["total"] = models.GetTagTotal(maps)

	// 返回值
	response := util.NewResponseSuccess()
	response.Data = data
	util.ResponseSuccess(c, response)
	// c.JSON(http.StatusOK, gin.H{
	// 	"code": code,
	// 	"msg":  e.GetMsg(code),
	// 	"data": data,
	// })
}

// 新增文章标签
func AddTags(c *gin.Context) {
	// PostForm 从表单获取数据
	name := c.PostForm("name")
	state := com.StrTo(c.DefaultPostForm("state", "0")).MustInt()
	createdBy := c.PostForm("created_by")
	// 校验数据
	valid := validation.Validation{}
	valid.Required(name, "name").Message("标签名称不能为空")
	valid.MaxSize(name, 100, "name").Message("标签名称长度不能超过100")
	valid.Required(createdBy, "created_by").Message("创建人不能为空")
	valid.MaxSize(createdBy, 100, "created_by").Message("创建人长度不能超过100")
	valid.Range(state, 0, 1, "state").Message("状态只允许0或1")

	data := make(map[string]interface{})
	incrementId := 0

	// 校验结果
	code := e.INVALID_PARAMS
	returnMsg := e.GetMsg(code)
	if !valid.HasErrors() {
		if !models.ExistsTagByName(name) {
			code = e.SUCCESS
			returnMsg = e.GetMsg(code)
			incrementId = models.AddTag(name, state, createdBy)
		} else {
			code = e.ERROR_EXIST_TAG
			returnMsg = e.GetMsg(code)
		}
	} else {
		// 验证没通过
		for _, err := range valid.Errors {
			returnMsg = err.Key + " " + err.Message
		}
	}

	// 返回结果
	data["id"] = incrementId
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  returnMsg,
		"data": data,
	})
}

// 修改文章标签
func EditTags(c *gin.Context) {
	// PostForm 从表单获取数据
	id := com.StrTo(c.Param("id")).MustInt()
	name := c.PostForm("name")
	modifiedBy := c.PostForm("modified_by")
	createdBy := c.PostForm("created_by")
	state := com.StrTo(c.DefaultPostForm("state", "0")).MustInt()

	tagData := models.Tag{
		Name:       name,
		ModifiedBy: modifiedBy,
		State:      state,
		CreatedBy:  createdBy,
	}

	// 数据校验, 结构体的校验
	err := customValidator.Validate(tagData)
	if err != nil {
		// 所有错误：err.Error()
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "failed:: " + err.Error(),
			"data": make(map[string]interface{}),
		})
	} else {
		models.EditTag(id, tagData)

		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "success",
			"data": make(map[string]interface{}),
		})
	}

}

// 删除文章标签
func DeleteTags(c *gin.Context) {
	// 从url获取id参数
	id := com.StrTo(c.Param("id")).MustInt()
	if id <= 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "id不能小于0",
			"data": make(map[string]interface{}),
		})
	}

	// 执行删除操作
	returnMsg := "删除成功"
	if ret := models.DeleteTAg(id); ret == false {
		returnMsg = "删除失败"
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  returnMsg,
		"data": make(map[string]interface{}),
	})
}
